<template>
    <div>
        <HTable :tableData="tableData" :tableLabel="tableLabel" :tableAttrs="tableAttrs"/>
    </div>
</template>

<script>
import HTable from '@/components/HTable.vue'
import filters from '@/utils/filters.js'
import { findProjects, findCommitInfo } from './api/index.js'
export default {
    name: 'today-commit',
    components: {
        HTable
    },
    data () {
        return {
            tableLabel: [
                { label: '提交人', prop: 'author_name' },
                { label: '应用', prop: 'projectName' },
                { label: '提交信息', prop: 'message' }
            ],
            tableData: [],
            tableAttrs: {
                isMultiple: false,
                isPagination: false,
                isAction: false
            }
        }
    },
    methods: {
        async findCommitInfo () {
            let { data: projects } = await findProjects()
            // projects = projects.concat([
            //     { id: '16', name: 'app-ios' },
            //     { id: '63', name: 'app-android' }
            // ])
            const reqArr = projects.map(item => findCommitInfo({
                projectId: item.id,
                since: filters.formatDate(Date.now(), 'YYYY-MM-DD') + 'T00:00:00Z',
                all: true
            }))
            Promise.all(reqArr).then(result => {
                const data = result.map((item, index) => {
                    let itemData = item.data.map(obj => {
                        obj.projectName = projects[index].name
                        return obj
                    })
                    return itemData
                }).flat()
                this.tableData = data.filter(item => item.message.indexOf('Merge branch') == -1)
            })
        }
    },
    mounted () {
        this.findCommitInfo()
    }
}
</script>